const fs = require('fs')
const path = require('path')

const dataRoot = './data/CN'
function updateRegion (parentDir) {
  const subs = fs.readdirSync(dataRoot + parentDir, { withFileTypes: true });

  for (let file of subs) {
    if (file.isDirectory()) {
      updateRegion(parentDir + '/' + file.name);
    } else {
      let path = dataRoot + parentDir + '/' + file.name
      let encoding = 'utf8'
      // 获取数据
      let data = fs.readFileSync(path, { encoding })

      // 数据处理
      let arr = JSON.parse(data)
      let updatedData = arr.map(item => {
        return {
          code: item.code,
          cnName: item.cnName
        }
      })

      // 重写数据
      fs.writeFile(path, JSON.stringify(updatedData), { encoding }, err => {
       if (err != null) {
         console.log('writefFile', err)
        }
      })
    }
  }
}

updateRegion('')